#include "o3_transmitcontent_h.h"

O3_TransmitContent_H::O3_TransmitContent_H(QWidget *parent) : QMainWindow(parent)
{
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(630,430);
    ui_Combox_Init();
    ui_Label_Init();
    ui_Btn_Init();
   connect_Init();
   Sysctrl->SYS_CTL_MODE = 0;//自动控制
//   if(trans_O3!=0 && trans_O3 !=10)
//   {
//       ui->label_O3density->setNum(Sysset->Ozo_Span[trans_O3-1]);
//       ui->label_densityNum->setNum(trans_O3);
//   }
}
void O3_TransmitContent_H::connect_Init()
{
    //第一个选择下拉菜单
    connect(theComboBox,&MyComboBox::clicked,[=](){
        {
            theComboBox->showPopup();
            theComboBox->view()->setFocus();
            flag=1;
        }

    });

    connect(theComboBox,&QComboBox::currentTextChanged,[=](){
        {
            theLabel[1]->setText(theComboBox->currentText());
            ui_renew_Init();
        }
    });

    //确定
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){
        trans_O3=(theComboBox->currentIndex() - 1);
        theLabel[4]->show();    //按了确定键后显示运行页面
        theLabel[5]->show();
        theLabel[6]->show();
        theLabel[7]->show();
        theBtn[2]->show();
        theLabel[9]->show();
        if(theComboBox->currentIndex() == theComboBox->count()-1)
        {
            Sysset->IS_OZO_ENABLE=1;
            Sysctrl->Sys_Gas_Sel = 1;
            Sysctrl->Sys_Span_Sel=8;
            Sysctrl->Sys_Ozo_Sel = Sysset->Sys_Ozo_Sel = 7;
            Sysctrl->Total_Flow = Sysset->Ozo_ZeroAir;
            Sysctrl->Target_Consentration=0;
            Sysctrl->BD_Ozonator.Vot_Set = Sysset->Ozo_Span[6]*0.132+5.7;
            Sysctrl->Para_Save =1;
            O3_alone=1;
        }
        else
        {
            Sysset->IS_OZO_ENABLE=1;
            Sysctrl->Sys_Gas_Sel = Sysset->Sys_Gas_Sel= 1;
            Sysctrl->Sys_Span_Sel=Sysset->Sys_Span_sel =8;
            Sysctrl->Sys_Ozo_Sel = Sysset->Sys_Ozo_Sel=Sysset->Sys_Ozo_Sel = trans_O3;
            Sysctrl->Total_Flow = Sysset->Ozo_ZeroAir;
            Sysctrl->Target_Consentration=0;
            Sysctrl->BD_Ozonator.Vot_Set = Sysset->Ozo_Span[trans_O3-1]*0.132+5.7;
            Sysset->Sys_Only_Ozo= O3_alone=1;
            Sysctrl->Para_Save = 1;
        }


    });

    //返回
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
    });

    //修改手动时的数据
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){

            keyPad *board = new keyPad(this);
             board->show();
            connect(board,&keyPad::changename2,this,&O3_TransmitContent_H::Recive_Gas_SelData);
            connect(board,&keyPad::destroyed,[=](){
                btnlist_ui.at(2)->setFocus();
            });
    });
}

void O3_TransmitContent_H::Recive_Gas_SelData(QString s)
{
    if(s.toFloat()>=0 && s.toFloat()<=100)
    {
        Sysset->Ozo_Span[6] = s.toFloat();
    }
    else if(s.toFloat() <0)
    {
        Sysset->Ozo_Span[6] = 0;
    }
    else if(s.toFloat() >100)
    {
        Sysset->Ozo_Span[6] = 100;
    }
    btnlist_ui.at(2)->setText(QString::number(Sysset->Ozo_Span[6],'f',1));
}

void O3_TransmitContent_H::ui_renew_Init()
{
    if(theComboBox->currentIndex() == theComboBox->count()-1)
    {
        theLabel[4]->setText(tr("当前值："));
        theLabel[5]->setText(QString::number(Sysset->Ozo_Span[6],'f',1));
        theLabel[6]->setText(tr("%"));
        theLabel[7]->setText(tr("设置值："));
        theLabel[9]->setText(tr("SCCM"));
        btnlist_ui.at(2)->setText(QString::number(Sysset->Ozo_Span[6],'f',1));
        btnlist_ui.at(2)->setEnabled(true);
    }
    else
    {
        theLabel[4]->setText(tr("O3浓度："));
        theLabel[5]->setText(QString::number(Sysset->Gas[temp_Gas].gasconcentration_span[temp_Gas_select],'f',1));
        theLabel[6]->setText(tr("%"));
        theLabel[7]->setText(tr("总流量："));
        theLabel[9]->setText(tr("SCCM"));
        btnlist_ui.at(2)->setText(QString::number(Sysset->Gas[temp_Gas].totalflow_span[temp_Gas_select],'f',1));
        btnlist_ui.at(2)->setEnabled(false);
    }
    theLabel[2]->hide();
    theLabel[3]->hide();
    theLabel[4]->hide();
    theLabel[5]->hide();
    theLabel[6]->hide();
    theLabel[7]->hide();
    theLabel[8]->hide();
    theLabel[9]->hide();
    btnlist_ui.at(2)->hide();

}
bool O3_TransmitContent_H::eventFilter(QObject *watched, QEvent *event)
{

//            key_Event::key_Event1(pos,watched, event, btnlist, btnlist_ui);
//          for(int i=0;i<btnlist_ui.length();i++)
            if(btnlist_ui.at(0) == watched||btnlist_ui.at(1) == watched||btnlist_ui.at(2) == watched||theComboBox == watched||theComboBox->view() == watched)
        {
            if(event->type() == QEvent::KeyPress)
            {
                //qDebug()<<focusWidget()->objectName();
                QKeyEvent *ev = (QKeyEvent *)event;
                QPushButton *button = (QPushButton *)watched;
                MyComboBox *boBox = (MyComboBox *)watched;

                if(flag==1)//当下拉显示的时候
               {
                   switch (ev->key()) {
                   case UP:
                   {
                       //qDebug()<<"I";
                       if(focusWidget()== theComboBox )
                       {
                           if(theComboBox->currentIndex()>0)
                               theComboBox->setCurrentIndex(theComboBox->currentIndex() - 1);
                           else
                               theComboBox->setCurrentIndex(theComboBox->count()-1);
                               theComboBox->showPopup();
                       }
                       break;
                   }
                   case DOWN:
                   {
                       //qDebug()<<"K";
                       if(focusWidget()== theComboBox)
                       {
                           if(theComboBox->currentIndex() < theComboBox->count()-1)
                               theComboBox->setCurrentIndex(theComboBox->currentIndex()+1);
                           else
                               theComboBox->setCurrentIndex(0);
                           theComboBox->showPopup();
                       }

                       break;
                   }
                   case OK:
                   {
                       if((flag==1))
                       {
                           theComboBox->hidePopup();
                           flag=2;
                       }
                       else
                       {
                           emit  button-> clicked();
                           emit boBox-> clicked();
                       }
                        break;
                   }
                   default:
                       break;
                   }
               }
               else
               {
                   switch (ev->key())
                   {
                   case OK:
                   {

                       emit  button-> clicked();
                       emit  boBox-> clicked();
                     break;
                   }
                   case UP: //Up
                     {
//                         if(focusWidget() != btnlist_ui.first())
                            focusPreviousChild();
                       break;
                     }
                   case DOWN: //Down
                     {
//                         if(focusWidget() != btnlist_ui.back())
                            focusNextChild();
                       break;
                     }
                   case BACK:
                   {
                       this->parentWidget()->show();
                       this->deleteLater();
                       break;
                   }
                   case INIT:
                   {
                       init_Show *show = new init_Show(this);
                       show->show();
                       this->hide();

                       break;
                   }
                    default:
                       break;
                   }
               }
            }
            }
}

void O3_TransmitContent_H::ui_Label_Init()
{

    for(int i=0;i<13;i++)//创建界面显示的label
    {
         theLabel[i] = new QLabel(this);
         theLabel[i]->setStyleSheet("QLabel{\
                                outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                                border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                border-style: outset;\
                                /* 按钮文本设置 */\
                                qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                font-size: 20px;\
                                color: #123567;			                 /* 文本颜色 */\
                           }"
          );
    }
    theLabel[0]->setGeometry(50,200,200,30);//设定每隔放置一个按键
    theLabel[1]->setGeometry(300,200,100,30);//设定每隔放置一个按键
//    theLabel[2]->setGeometry(50,200,200,30);//设定每隔放置一个按键
//    theLabel[3]->setGeometry(300,200,100,30);//设定每隔放置一个按键
    theLabel[4]->setGeometry(50,240,200,30);//设定每隔放置一个按键
    theLabel[5]->setGeometry(300,240,100,30);//设定每隔放置一个按键
    theLabel[6]->setGeometry(420,240,100,30);//设定每隔放置一个按键
    theLabel[7]->setGeometry(50,280,200,30);//设定每隔放置一个按键
    theLabel[8]->setGeometry(300,280,100,30);//设定每隔放置一个按键
    theLabel[9]->setGeometry(420,280,100,30);//设定每隔放置一个按键
    theLabel[10]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
    theLabel[10]->setStyleSheet("QLabel{"
                          + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
    theLabel[11]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
    theLabel[12]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

   theLabel[10]->setGeometry(0,0,640,60);//设定每隔放置一个按键
   theLabel[11]->setGeometry(0,0,300,60);//设定每隔放置一个按键
   theLabel[12]->setGeometry(340,0,300,60);//设定每隔放置一个按键
   theLabel[10]->setText("传递标准");

    theLabel[0]->setText(tr("当前选择的浓度为："));
    theLabel[1]->setText(theComboBox->currentText());
//    theLabel[2]->setText(tr("当前选择的跨度为："));
//    theLabel[3]->setText(theComboBox2->currentText());
    theLabel[2]->hide();
    theLabel[3]->hide();
    theLabel[4]->hide();
    theLabel[5]->hide();
    theLabel[6]->hide();
    theLabel[7]->hide();
    theLabel[8]->hide();
    theLabel[9]->hide();


}

void O3_TransmitContent_H::ui_Combox_Init()
{
    {

        QStringList strList;
    strList<<"浓度1"<<"浓度2"<<"浓度3"<<"浓度4"<<"浓度5"<<"浓度6"<<"手动";

    theComboBox = new MyComboBox(this);

    theComboBox->addItems(strList);
    theComboBox->setGeometry(130,100,150,60 );
    theComboBox->installEventFilter(this);
    theComboBox->view()->installEventFilter(this);

    theComboBox->setStyleSheet("QComboBox{color:white;      /* 字体颜色*/\
                               background-color: rgb(39, 170, 255);\
                               border-radius:4px;\
                               border: 1px groove white; \
                               padding:1px 30px;\
                                border-style: outset;}"
    "QComboBox:focus{color:rgb(250,250, 250);\
                                background-color: rgb(255, 170, 39);\
                                border-radius:4px;\
                                border: 2px rgb(110, 111, 215); \
                                padding:1px 30px;\
                                border-style: outset;}"
    );

    }

}

void O3_TransmitContent_H::ui_Btn_Init()
{

    for(int i=0;i<3;i++)//创建主屏幕显示的按键
    {

         theBtn[i] = new QPushButton(this);
//        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
//         theBtn[i]->setFixedSize(160,60);
        theBtn[i]->geometry();
}

    theBtn[0]->setStyleSheet(  "QPushButton{"+theBox_style[0]+ "background-image: url(:/pic/切换.png); /* 背景图片 */"+"}"
     "QPushButton:focus{"+theBox_style[1]+ "background-image: url(:/pic/切换2.png); /* 背景图片 */"+"}");
    theBtn[1]->setStyleSheet(  "QPushButton{"+theBox_style[0]+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
     "QPushButton:focus{"+theBox_style[1]+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");
    theBtn[2]->setStyleSheet(  "QPushButton{"+theBox_style[0]+ "background-image: url(:/pic/.png); /* 背景图片 */"+"}"
     "QPushButton:focus{"+theBox_style[1]+ "background-image: url(:/pic/.png); /* 背景图片 */"+"}");

  btnlist_ui.append(theBtn[0]);
btnlist_ui.append(theBtn[1]);
btnlist_ui.append(theBtn[2]);

btnlist_ui.at(0)->setGeometry(100,360,160,60 );
btnlist_ui.at(1)->setGeometry(390,360,160,60 );
btnlist_ui.at(2)->setGeometry(300,280,100,30 );
btnlist_ui.at(0)->setText(tr("  确定"));
btnlist_ui.at(1)->setText(tr("  返回"));
btnlist_ui.at(2)->setText(tr("修改"));
btnlist_ui.at(2)->setEnabled(true);
btnlist_ui.at(2)->hide();

//事件过滤器
foreach (QPushButton *btn, btnlist_ui) {
    btn->installEventFilter(this);
}
}

